/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.TourTypeDTO;
import east2west.interfaces.TourTypeInterfacE;
import east2west.util.DatabaseUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author thanhluan
 */
public class TourTypeDAO implements TourTypeInterfacE {

    Connection con = null;
    CallableStatement call = null;
    ResultSet rs = null;

    

    public ArrayList<TourTypeDTO> findByName(String typeName) {
        try {
            con = DatabaseUtil.getConnection();
            ArrayList<TourTypeDTO> list = new ArrayList<TourTypeDTO>();
            call = con.prepareCall("{call sp_Call_View_TourType_FindName(?)}");
            call.setString(1, typeName);
            rs = call.executeQuery();
            while (rs.next()) {
                TourTypeDTO dto = new TourTypeDTO();

                dto.setTourTypeId(rs.getInt(1));
                dto.setTourTypeName(rs.getString(2));
                 dto.setIsDelete(rs.getBoolean(3));
                //dto.setIsDelete((byte) rs.getInt(3));
                list.add(dto);
            }
            return list;
        } catch (Exception ex) {
            return null;

        }
    }

    public ArrayList<TourTypeDTO> findByDto(TourTypeDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TourTypeDTO findById(int id) {
        try {
            con = DatabaseUtil.getConnection();
            TourTypeDTO dto = null;
            call = con.prepareCall("{call sp_Call_View_TourType_FindId(?)}");
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {

                dto = new TourTypeDTO();
                dto.setTourTypeId(rs.getInt(1));
                dto.setTourTypeName(rs.getString(2));
                 dto.setIsDelete(rs.getBoolean(3));
                //dto.setIsDelete((byte) rs.getInt(3));
            }
            return dto;
        } catch (Exception ex) {
            return null;

        }
    }

    public boolean create(TourTypeDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            PreparedStatement prt = con.prepareStatement("INSERT INTO TourTypes(TypeName) VALUES(?)");
            prt.setString(1, dto.getTourTypeName());
            prt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean update(TourTypeDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Update_TourType(?,?)}");
            call.setInt(1, dto.getTourTypeId());
            call.setString(2, dto.getTourTypeName());
            call.executeUpdate();
            return true;
        } catch (SQLException ex) {
 
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean delete(TourTypeDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            con.setAutoCommit(false);
            call = con.prepareCall("{call sp_Delete_TourType(?)}");
            call.setInt(1, id);
            call.executeUpdate();
            con.commit();
            return true;
        } catch (SQLException ex) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex1) {
                }
            }
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public ArrayList<TourTypeDTO> readByAll() {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("sp_Call_View_TourType");
            ArrayList<TourTypeDTO> listType = new ArrayList<TourTypeDTO>();
            rs = call.executeQuery();
            while(rs.next() ){
                TourTypeDTO dto = new TourTypeDTO();
               dto.setTourTypeId(rs.getInt(1));
                dto.setTourTypeName(rs.getString(2));
                dto.setIsDelete(rs.getBoolean(3));
                listType.add(dto);
            }
            return listType;
        } catch (SQLException ex) {
           return null;
        }

    }
}
